package com.bank.seckill.controller;

import com.bank.entity.Member;
import com.bank.entity.Seckill;
import com.bank.entity.constant.ResponseEnum;
import com.bank.entity.vo.Response;
import com.bank.seckill.service.impl.GoodsServiceImpl;
import com.bank.um.service.Impl.MemberServiceImpl;
import com.dtflys.forest.annotation.Get;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

/**
 * @author: Lixy
 * @email: 1908598362@qq.com
 * @date: 2022/1/27 星期四 21:56
 */

@RestController
@RequestMapping("/goods")
@CrossOrigin
public class GoodsController {

    @Autowired
    private MemberServiceImpl memberService;

    @Autowired
    private GoodsServiceImpl goodsService;

    @RequestMapping("/toPageList")
    public String pageList(Model model,Member member) {
//        if (StringUtils.isNullOrEmpty(uuid)) {
//            return "login";
//        }
//        Member member = memberService.SelectByCoolredis(uuid, response, request);
//        if (member == null) {
//            return "login";
//        }
        model.addAttribute("member", member);
        return "seckill";
    }

    @PostMapping("/addGoods")
    public Response addGoods(Seckill goods,MultipartFile image) throws IOException {
        String fullPath = goodsService.saveGoods(goods,image);
        return Response.response1(ResponseEnum.SUCCESS,fullPath,"商品上传成功");
    }

    @PostMapping("/selectGoods")
    public Response selectGoods(String secId,String name){
        Seckill goods1 = null;
        if (secId==null){
            goods1 = goodsService.SelcetGoodsByName(name);
        }else if (name==null){
            goods1 = goodsService.SelcetGoodsById(secId);
        }else {
            goods1 = goodsService.SelcetGoodsByName(name);}
        if (goods1!=null){
            return Response.response2(ResponseEnum.SUCCESS,goods1);
        }else {
            return Response.response3(ResponseEnum.GOODS_HAS_BY_CANNCELED,ResponseEnum.GOODS_HAS_BY_CANNCELED.getMsg());
        }
    }

    @GetMapping("/all")
    public List<Seckill> findAll(){return goodsService.SelectAll();}

}
